package top.dancun.easteacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.dancun.common.utils.Constant;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;
import top.dancun.easteacher.dao.EvaluationTeachingDao;
import top.dancun.easteacher.entity.EvaluationTeachingEntity;
import top.dancun.easteacher.feign.service.CourseFeignService;
import top.dancun.easteacher.service.EvaluationTeachingService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//TODO remark暂时废弃，后期添加查询
/**
 * @author dancun
 */
@Service("evaluationTeachingService")
public class EvaluationTeachingServiceImpl extends ServiceImpl<EvaluationTeachingDao, EvaluationTeachingEntity> implements EvaluationTeachingService {

    @Autowired
    private CourseFeignService courseFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EvaluationTeachingEntity> page = this.page(
                new Query<EvaluationTeachingEntity>().getPage(params),
                new QueryWrapper<EvaluationTeachingEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询教师综合评价，按学期
     * @param params
     * @return
     */
    @Override
    public PageUtils getByTid(Map<String, String> params) {
        Integer pageIndex = Integer.parseInt(params.get(Constant.PAGE));
        Integer limit = Integer.parseInt(params.get(Constant.LIMIT));
        QueryWrapper<EvaluationTeachingEntity> wrapper = Wrappers.query();
        //LambdaQuery获取不到非数据字段缓存
        wrapper.orderByDesc("levelAvg").last("limit "+pageIndex+" ,"+limit).lambda()
                .eq(StringUtils.isNotBlank(params.get("teacherId")),
                        EvaluationTeachingEntity::getTeacherId,
                        params.get("teacherId"))
                .eq(StringUtils.isNotBlank(params.get("semester")),
                        EvaluationTeachingEntity::getSemester,
                        params.get("semester"));


        List<EvaluationTeachingEntity> list = baseMapper.groupByTidAndSemester(wrapper);
        List<Integer> courseIdList = new ArrayList<>();
        //获取所有课程id
        list.forEach(evaluationTeachingEntity -> {
            courseIdList.add(evaluationTeachingEntity.getCourseId());
        });
        //远程获取课程信息并添加课程名称到评教
        Map<Integer,String> courseNameMap = courseFeignService.getCourseByIds(courseIdList);
        list.forEach(evaluationTeachingEntity -> {
            evaluationTeachingEntity.setCourseName(courseNameMap.get(evaluationTeachingEntity.getCourseId()));
        });
        Page<EvaluationTeachingEntity> page =new Page<>(pageIndex,limit);
        page.setRecords(list);
        PageUtils pageUtils = new PageUtils(page);

        return pageUtils;
    }

    /**
     * 获取所有教师所有学期综合评价
     * @param params
     * @return
     */

    @Override
    public PageUtils getAll(Map<String, String> params) {
        IPage page =  new Page(Integer.parseInt(params.get(Constant.PAGE)),Integer.parseInt(params.get(Constant.LIMIT)));
        page =  baseMapper.getAllGroupByTidAndSemester(page);

        return new PageUtils(page);
    }

    @Override
    public PageUtils getTop() {
        IPage page =  new Page(1,20);
        page =  baseMapper.getAllGroupByTidAndSemester(page);

        return new PageUtils(page);
    }

    /**
     *学生查询自己对老师评教情况
     * @param params
     * @return
     */
    @Override
    public EvaluationTeachingEntity getBySTS(Map<String, String> params) {
        EvaluationTeachingEntity evaluationTeaching ;
        evaluationTeaching = lambdaQuery().eq(EvaluationTeachingEntity::getStudentId,params.get("studentId"))
                .eq(EvaluationTeachingEntity::getTeacherId,params.get("teacherId"))
                .eq(EvaluationTeachingEntity::getCourseId,params.get("courseId"))
                .eq(EvaluationTeachingEntity::getSemester,params.get("semester")).one();

        return evaluationTeaching;
    }

}